#include "ShowScreenWindow.h"
#include "MyDx11.h"

ShowScreenWindow::ShowScreenWindow(QWidget* parent)
	: QWidget(parent)
{
	QWidget::setAttribute(Qt::WA_PaintOnScreen); // 允许DX渲染 ...
	setFocusPolicy(Qt::WheelFocus); // 允许按键时间 ...
	setMouseTracking(true); // 允许鼠标移动 ...
	this->resize(parent->size());
	this->initD3D();
	this->setVisible(false);
}

void ShowScreenWindow::addMonitor(int _monitor_width, int _monitor_height)
{
	if (this->m_d3d11 != nullptr) {
		this->m_d3d11->createMonitorTexture(_monitor_width, _monitor_height);
	}
}

void ShowScreenWindow::removeMonitor()
{
	if (this->m_d3d11 != nullptr) {
		this->m_d3d11->removeMonitorTexture();
	}
}

void ShowScreenWindow::rend() const
{
	this->m_d3d11->render();
}

void ShowScreenWindow::copyMonitor(ID3D11Device* _device, ID3D11DeviceContext* _context, ID3D11Texture2D* _monitor_texture)
{
	this->m_d3d11->copyData(_device, _context, _monitor_texture, "monitor");
}

void ShowScreenWindow::renderToTexture(bool _draw_cursor)
{
	this->m_d3d11->renderToTexture(_draw_cursor);
}

void ShowScreenWindow::copyFromRenderTarget(uint8_t* _buffer, size_t _size)
{
	this->m_d3d11->copyFromRenderTarget(_buffer, _size);
}

void ShowScreenWindow::copyToRenderTarget(uint8_t* _buffer, size_t _size)
{
	this->m_d3d11->copyData(this->m_d3d11, _buffer, _size, "monitor");
}

ShowScreenWindow::~ShowScreenWindow()
{
	this->removeMonitor();
	delete this->m_d3d11;
}

void ShowScreenWindow::paintEvent(QPaintEvent* event)
{
	// this->m_d3d11->render();
}

void ShowScreenWindow::resizeEvent(QResizeEvent* event)
{
	this->resizeD3D();
}

void ShowScreenWindow::initD3D()
{
	delete this->m_d3d11;
	this->m_d3d11 = new MyDx11(reinterpret_cast<HWND>(this->winId()), this->width(), this->height());
}


void ShowScreenWindow::resizeD3D()
{
	// this->initD3D();
}
